package engeener.clientapp.tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Connector {
	Connection conn;
	private String url = "jdbc:mysql://25.102.168.53:3307/automats";
	private String username = "wojtek";
	private String password = "test";
	private String driverName = "com.mysql.jdbc.Driver";
	private Statement stmt;

	public Connector() {
		System.out.println("Creating new connection...");
		try {
			Class.forName(driverName).newInstance();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(1);
		}
		 System.out.print(" Driver -  OK");

		try {
			conn = DriverManager.getConnection(url, username, password);
		} catch (SQLException e) {
			System.out.println("Error with connect to Database");
			e.printStackTrace();
			System.exit(1);

		}
		 System.out.print("\nDatabase -  OK");
	}

	public Connection getConnection() {
		return conn;
	}

	public Statement getStatement() {

		try {
			stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
					ResultSet.CONCUR_UPDATABLE);
		} catch (SQLException e) {
			System.err.println();
		}

		return stmt;
	}
	
	public void closeConnection() throws SQLException{
		this.conn.close(); 
		System.out.println("Connection close");
		}
}
